<?php
/* ******************************
	Incoming Tasks aJax actions
   ******************************
*/
include('../includes/classes/core.php');
$action 	= $_REQUEST['act'];
$data 		= '';
$error		= '';
$start		= $_REQUEST['start'];
$end		= $_REQUEST['end'];
$user_id	= $_SESSION['USERID1'];
$count 		= $_REQUEST['count'];

switch ($action) {
	case 'get_list':
		if ($_REQUEST['tab']=='0'){
    	    $rResult = mysql_query("SELECT  warehouse_detail.id,
                            				b_letter_details.barcode,
                            				production.`name` AS `production`,
                            				CONCAT('ბ - ', b_letter_details.a_letter_id) AS `letter`,
                            				a_letter.fact_end_date,
                            				IF(max(warehouse_detail.type)=2,1,0),
                            				COUNT(part1.id) AS toneti,
                            				COUNT(part2.id) AS barabani,
                            				COUNT(part3.id) AS magnituri,
                            				COUNT(part4.id) as rezini,
                            				COUNT(part5.id) as gamwmendi,
                            				COUNT(part6.id) as dozirebis,
                            				COUNT(part7.id) AS chipi,
                            				COUNT(part8.id) AS korpusi,
                            				COUNT(part9.id) AS not_service,
                            				sum(IF(warehouse_detail=2,0,b_letter_details.price))

                            FROM `b_letter_details`
                            JOIN  a_letter on b_letter_details.a_letter_id=a_letter.id
                            LEFT JOIN  production ON SUBSTR(b_letter_details.barcode, 6, 4) = production.id
                            LEFT JOIN  client ON SUBSTR(b_letter_details.barcode, 2, 4) = client.id
                            LEFT JOIN  warehouse_detail ON b_letter_details.warehouse_detail_id=warehouse_detail.id
                            LEFT JOIN  b_letter_details part1 ON  part1.part_type_id=2  AND part1.id =b_letter_details.id
                            LEFT JOIN  b_letter_details part2 ON  part2.part_type_id=3  AND part2.id =b_letter_details.id
                            LEFT JOIN  b_letter_details part3 ON  part3.part_type_id=4  AND part3.id =b_letter_details.id
                            LEFT JOIN  b_letter_details part4 ON  part4.part_type_id=5  AND part4.id =b_letter_details.id
                            LEFT JOIN  b_letter_details part5 ON  part5.part_type_id=6  AND part5.id =b_letter_details.id
                            LEFT JOIN  b_letter_details part6 ON  part6.part_type_id=7  AND part6.id =b_letter_details.id
                            LEFT JOIN  b_letter_details part7 ON  part7.part_type_id=8  AND part7.id =b_letter_details.id
                            LEFT JOIN  b_letter_details part8 ON  part8.part_type_id=9  AND part8.id =b_letter_details.id
                            LEFT JOIN  b_letter_details part9 ON  part9.part_type_id=16 AND part9.id =b_letter_details.id
					    	     JOIN client_web_users  ON  client_web_users.client_id=client.id AND client_web_users.id=$user_id
					WHERE      DATE(a_letter.fact_end_date) BETWEEN '$start' AND '$end'
					GROUP BY   b_letter_details.a_letter_id, b_letter_details.barcode");
		}else{
			$rResult=mysql_query("
                       SELECT 	warehouse_detail.id,
                        		barcodes.serial_number,
                        		production.`name`,
                        		CONCAT('ე - ', e_letter_details.d_letter_id) AS `letter`,
                        		DATE(d_letter.fact_end_date) AS `date`,
                        		printer_parts_type.`name` AS `service`,
                        		IF(warehouse_detail.type = 2, 0, e_letter_details.price)
                        FROM    e_letter_details
                        JOIN    warehouse_detail ON warehouse_detail.id	= e_letter_details.warehouse_detail_id AND warehouse_detail.actived=1
                        JOIN	d_letter ON d_letter.id   = e_letter_details.d_letter_id and `d_letter`.`status` >3
                        JOIN	barcodes ON warehouse_detail.barcode =barcodes.`code`
                        JOIN	production ON production.id = warehouse_detail.prod_id
                        JOIN	printer_parts_type ON printer_parts_type.id = e_letter_details.part_type_id
                        JOIN	client_objects ON client_objects.id = d_letter.client_object_id
                        JOIN	client ON client.id = client_objects.client_id
                        JOIN    client_web_users  ON  client_web_users.client_id=client.id AND client_web_users.id=$user_id			    
                        WHERE DATE(d_letter.fact_end_date) BETWEEN '$start' AND '$end'			    
                        	");

		}

		$data = array(
			"aaData"	=> array()
		);

		while ( $aRow = mysql_fetch_array( $rResult ) )
		{

			$data['aaData'][] = $aRow;
		}
        break;
    default:
       $error = "action null";
}
$data['error'] = $error;
echo json_encode($data);

?>